<?php

/**
 * 异常处理的基类
 * http://my.oschina.net/mingtingling/blog/92104(怎么一步步编写简单的PHP的Framework-九)
 *
 *
 */
class BaseException extends Exception
{
    public function __construct($message = "", $code = 0, Exception $previous = null) {
        parent::__construct($message, $code, $previous);
        if (true === C('appDebug')) {
            echo "<pre>";
        }
    }

    public function __destruct()
    {
        if (true === C('appDebug')) {
            echo "</pre>";
        }
    }

    public function printStack()
    {
        if (true === C('debug')) {
            echo "Message : " . parent::getMessage() . parent::getTraceAsString();
        } else {
            $this->_toLogFile("Message : " . parent::getMessage() . parent::getTraceAsString());
            $this->_outputErrorPage();
        }
    }

    public function __toString()
    {
        if (true !== C('debug')) {
            $this->_toLogFile("Message : " . parent::getMessage() . parent::getTraceAsString());
            $this->_outputErrorPage();
            exit();
        }
        return parent::__toString();
    }

    protected function _toLogFile($str)
    {
        Logger::log($str);
    }

    protected function _outputErrorPage()
    {
        header("content-type:text/html");
        echo file_get_contents(APP_PATH . '/error.html');
    }
}